import socket
import time
import threading
import os
from Crypto.Cipher import AES

ss = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
ss.bind(('', 8890))

cs = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
cs.bind(('', 4002))

key = os.urandom(32)

def aes_encrypt(plain):
    enc = AES.new(key, AES.MODE_CBC)
    fill = (16 - len(plain) % 16) * ' '
    return enc.encrypt(plain + fill)

def th_server():
    while True:
        rec = ss.recvfrom(1000)[0]
        dec = AES.new(key, AES.MODE_CBC)
        print dec.decrypt(rec)

def th_client():
    while True:
        cs.sendto(aes_encrypt("hello"), ("127.0.0.1", 8890))
        time.sleep(5)

ths = threading.Thread(target=th_server, args=())
thc = threading.Thread(target=th_client, args=())

ths.start()
thc.start()

